<template>
  <div class="form-select product-type-quality-time">
    <div class="period-search-child product-type-quality-time-number">
      <number-search
        ref="elInput"
        type="text"
        :min="1"
        :max="99"
        :max-length="2"
        :value="qualityTimeNumber"
        :validator="validatorQualityTime"
        @input="numUpdate"
      ></number-search>
    </div>

    <div class="period-search-child">
      <el-select
        :id="`form_${field.fieldName}`"
        :placeholder="$t('common.placeholder.select')"
        :clearable="clearable"
        :multiple="isMulti"
        ref="elSelect"
        filterable
        :value="qualityTimeType"
        @change="input"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.text"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
  </div>
</template>

<script>
import FormMixin from '@src/component/form/mixin/form'
import NumberSearch from '@src/component/form/components/FormNumber/extend/NumberSearch.vue'
import { getProductTypeQualityTimeOptions } from '@src/util/qualityInfoConfig'

export default {
  // eslint-disable-next-line vue/name-property-casing
  name: 'product-type-qualityTime-build',
  mixins: [FormMixin],
  props: {
    value: {
      type: Object | String,
      default: () => ({})
    },
    source: {
      type: Array,
    },
    clearable: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      newValue: this.value,
      params: {
        qualityTimeNumber: '',
        qualityTimeType: this.$t('common.base.year')
      },
    };
  },
  computed: {
    isMulti() {
      let setting = this.field.setting || {}
      return setting.isMulti
    },
    options() {
      return getProductTypeQualityTimeOptions()
    },
    qualityTimeNumber() {
      return this.value?.qualityTimeNumber || ''
    },
    qualityTimeType() {
      return this.value?.qualityTimeType || ''
    },
    selectDisabled() {
      return !this.qualityTimeNumber
    },
  },
  methods: {
    numUpdate(newValue){
      this.update({
        ...this.value,
        qualityTimeNumber: newValue,
      })
    },
    input(newValue) {
      this.update({
        ...this.value,
        qualityTimeType: newValue,
      })
    },
    update(newValue) {
      let oldValue = null
      this.$refs.elSelect.blur()
      
      this.$emit('update', { newValue, oldValue, field: this.field })
      this.$emit('input', newValue)
    },
    validatorQualityTime(value) {
      const numberReg = /^\d{1,}$/
      const qualityTimeReg = /^99$|^(?:\d|[1-9]\d|[0-9]\d)?$/
      if (!qualityTimeReg.test(value) || !numberReg.test(value)) {
        value = ''
      }
      
      return value
    }
  },
  components: {
    [NumberSearch.name]: NumberSearch
  }
};
</script>

<style lang="scss">
.product-type-quality-time {
  display: flex;
  .period-search-child + .period-search-child {
    margin-left: 10px;
  }
  .product-type-quality-time-number {
    width: 100%;
  }
}
</style>
